﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace linkouqs.Gamekit {

    public class Store : Inventory
    {
        private static Store _instance;
        public static Store Instance
        {
            get
            {
                if(_instance==null)
                {
                    _instance = GameObject.Find("StorePanel(Clone)").GetComponent<Store>();
                }
                return _instance;
            }
        }

        private Button sellBtn;
        private Button buyBtn;
     

        public int[] itemArray;

        public override void Start()
        {
            base.Start();

            sellBtn = transform.Find("SellBtn").GetComponent<Button>();
            buyBtn = transform.Find("BuyBtn").GetComponent<Button>();
            
            sellBtn.onClick.AddListener(SellItem);
            buyBtn.onClick.AddListener(BuyItem);

          

            InitShop();
        }
        
        private void InitShop()
        {
          
            foreach(int itemId in itemArray)
            {
                StoreItem(itemId);
            }
        }

        public void BuyItem(Item item)
        {
            bool isSuccess = InventoryManager.Instance.CosumeCoin(item.BuyPrice);
            if(isSuccess)
            {
                coinText.text = InventoryManager.Instance.CoinAmout.ToString();
                
                Knaspack.Instance.StoreItem(item);
            }

          
        }

        public void SellItem()
        {
            //TODO 出售物品
            coinText.text = InventoryManager.Instance.CoinAmout.ToString();
        }

        public void BuyItem()
        { 
            if(InventoryManager.Instance.selectSlot is StoreSlot)
            {
                BuyItem(InventoryManager.Instance.PickedItem.Item);
            }
            else
            {
                Debug.LogWarning("请选择你要买的商品");
            }
        }



      
    }

    
}
